<?
//售出股票


//查询满足条件的买入股票
$sqlbuy="select * from {$db_prefix}gp_market where username!='".$rs['username']."' and type=1 and state=0 and num>0 and price='$price' order by price asc";
$resultbuy=$db->query($sqlbuy);
while($rsbuy=$db->fetch_array($resultbuy)){
	//查询出交易数量
	if(intval($num)>0){
	if (intval($num)>$rsbuy['num']){
		//更新买入股票的数量和状态
		$sqlgx="update {$db_prefix}gp_market set num=0,state=1,cjtime='$curtime' where id='".$rsbuy['id']."'";
		$db->query($sqlgx);
		//更新售出股票的数量
		$sqlgx="update {$db_prefix}gp_market set num=num-'".$rsbuy['num']."' where id='".$curmarketid."'";
		$db->query($sqlgx);
		//插入交易记录中
		$sqljy="insert into {$db_prefix}gp_trade(username,username1,saleid,buyid,type,addtime,price,num) values('".$rs['username']."','".$rsbuy['username']."','".$curmarketid."','".$rsbuy['id']."',2,'".$curtime."','".$price."','".$rsbuy['num']."')";
		$db->query($sqljy);
		//更新卖出股票人的电子股
		$saleprice=$price*$rsbuy['num'];
		//交易收取费用
		$salepricefy=$saleprice*($glo_gp_traderate+$glo_gpdai*$glo_gprate)/100;
		$salepricesj=$saleprice*(1-($glo_gp_traderate+$glo_gpdai*$glo_gprate)/100);
		
		//进入慈善基金
		/*$csjjprice=$saleprice*$glo_gpjjrate/100;
		$sqlcsjj="update {$db_prefix}salaryset set csjjprice=csjjprice+'$csjjprice' where 1";
		$db->query($sqlcsjj);*/
		
		//30%进入电子股 70%进入电子钱包
		$salepricesj1=$salepricesj*$glo_gp_pricerate/100;
		$salepricesj2=$salepricesj*$glo_gp_pricerate1/100;
		
		//增加会员的电子股金额
		$sqlq="update {$db_prefix}users set gpprice=gpprice+'$salepricesj1',jjprice=jjprice+'$salepricesj2' where username='".$rs['username']."'";
		$db->query($sqlq);
		//插入到电子政务记录中 注册人的电子政务
		if($salepricesj1>0){
			//财务记录
			$e_userid=$rs['id'];$e_price=$salepricesj1;$e_type=2;$e_ptype=4;$e_addtime=$curtime;$e_memo="回购报单币";
			hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
		}
		if($salepricesj2>0){
			//财务记录
			$e_userid=$rs['id'];$e_price=$salepricesj2;$e_type=17;$e_ptype=3;$e_addtime=$curtime;$e_memo="回购报单币";
			hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
		}
		//更新买入股票的数量
		$sqlq1="update {$db_prefix}users set gpnum=gpnum+'".$rsbuy['num']."' where username='".$rsbuy['username']."'";
		$db->query($sqlq1);
		//更新剩余的数量
		$num=intval($num)-$rsbuy['num'];
	}else{
		//更新买入股票的数量
		$sqlgx="update {$db_prefix}gp_market set num=num-'".intval($num)."' where id='".$rsbuy['id']."'";
		$db->query($sqlgx);
		if (intval($num)==$rsbuy['num']){
			$sqlgx="update {$db_prefix}gp_market set state=1,cjtime='$curtime' where id='".$rsbuy['id']."'";
			$db->query($sqlgx);
		}
		//更新售出股票的数量和状态
		$sqlgx="update {$db_prefix}gp_market set num=0,state=1,cjtime='$curtime' where id='".$curmarketid."'";
		$db->query($sqlgx);
		//插入交易记录中
		$sqljy="insert into {$db_prefix}gp_trade(username,username1,saleid,buyid,type,addtime,price,num) values('".$rs['username']."','".$rsbuy['username']."','".$curmarketid."','".$rsbuy['id']."',2,'".$curtime."','".$price."','".intval($num)."')";
		$db->query($sqljy);
		//更新卖出股票人的电子股
		$saleprice=$price*intval($num);
		//交易收取费用
		$salepricefy=$saleprice*($glo_gp_traderate+$glo_gpdai*$glo_gprate)/100;
		$salepricesj=$saleprice*(1-($glo_gp_traderate+$glo_gpdai*$glo_gprate)/100);
		
		//进入慈善基金
		/*$csjjprice=$saleprice*$glo_gpjjrate/100;
		$sqlcsjj="update {$db_prefix}salaryset set csjjprice=csjjprice+'$csjjprice' where 1";
		$db->query($sqlcsjj);*/
		
		//30%进入电子股 70%进入电子钱包
		$salepricesj1=$salepricesj*$glo_gp_pricerate/100;
		$salepricesj2=$salepricesj*$glo_gp_pricerate1/100;
		
		//增加会员的电子股金额
		$sqlq="update {$db_prefix}users set gpprice=gpprice+'$salepricesj1',jjprice=jjprice+'$salepricesj2' where username='".$rs['username']."'";
		$db->query($sqlq);
		//插入到电子政务记录中 注册人的电子政务
		if($salepricesj1>0){
			//财务记录
			$e_userid=$rs['id'];$e_price=$salepricesj1;$e_type=2;$e_ptype=4;$e_addtime=$curtime;$e_memo="回购报单币";
			hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
		}
		if($salepricesj2>0){
			//财务记录
			$e_userid=$rs['id'];$e_price=$salepricesj2;$e_type=17;$e_ptype=3;$e_addtime=$curtime;$e_memo="回购报单币";
			hyepricejl($e_userid,$e_price,$e_type,$e_ptype,$e_addtime,$e_memo);
		}
		//更新买入股票的数量
		$sqlq1="update {$db_prefix}users set gpnum=gpnum+'".intval($num)."' where username='".$rsbuy['username']."'";
		$db->query($sqlq1);
		//更新剩余的数量
		$num=0;
	}
	}
}
$db->free_result($resultbuy);

//本次交易剩余的金额转到他的账户中
//系统暂时不开发此功能
?>